const express = require('express');
const db = require('../uilit/mysql');

const router=express.Router();

// 获取前端传递企业信息数据，将数据保存在公司表中
router.post('/addcompany',(req,res)=>{
    let indId,proId,cityId,areaId;
    let sqlIndId=`select ind_id from industry_category where ind_name='${req.body.industry}';`;
    let sqlProId=`select pro_id from province where pro_name='${req.body.cprovince}';`;
    let sqlCityId=`select city_id from city where city_name='${req.body.ccity}';`;
    let sqlAreaId=`select area_id from area where area_name='${req.body.carea}';`;
    db.query(sqlIndId,(err,data)=>{
        if(err){
            console.log(err);
        }else{
            indId=data[0].ind_id;
            
        }
    });
    db.query(sqlProId,(err,data)=>{
        if(err){
            console.log(err);
        }else{
            proId=data[0].pro_id;
            
        }
    });
    db.query(sqlCityId,(err,data)=>{
        if(err){
            console.log(err);
        }else{
            cityId=data[0].city_id;  
        }
    });
    db.query(sqlAreaId,(err,data)=>{
        if(err){
            console.log(err);
        }else{
            areaId=data[0].area_id;
            
        }
    });
    let sqladdcompany=`update company set c_name='${req.body.name}',ind_id=${indId},c_nature='${req.body.nature}',c_size='${req.body.size}',
                       pro_id=${proId},city_id=${cityId},area_id=${areaId},c_address='${req.body.address}',c_bname='${req.body.boss}',c_hrname='${req.body.person}',
                       c_hrtel='${req.body.tel}',c_hremail='${req.body.email}',c_intr='${req.body.introduction}' where c_id=${parseInt(req.body.id)};`;
    db.query(sqladdcompany,(err)=>{
        if(err){
            console.log(err);
        }else{
            res.send({code:res.statusCode});
        }
    });
});
module.exports=router;